package oop.ex3.messages;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import oop.ex3.sockets.ClientSocket;

public class FileContentMessage extends SimpleMessage{
	
	public static String[] _headers = {ValidHeaders.FILE};
	
	File _file;
	Long _size;
	
	public FileContentMessage(String msgName, File file){
		super(msgName);
		_file = file;
	}
	
	public FileContentMessage(String msgName){
		super(msgName);
	}
	
	public File getFile(){
		return _file;
	}
	
	public long getSize(){
		return _size;
	}
	
	public void sendMessage(ClientSocket socket) throws IOException{
		socket.writeToSocket(_header);
		socket.writeToSocket(_file.length());
		FileInputStream is = new FileInputStream(_file);
		for (int i = 0 ; i < _file.length() ; i++){
			socket.write(is.read());
		}
		is.close();
		System.out.println("OUT <to: "+socket.getPort()+"> "+_header);
	}
	
	public FileContentMessage readMessage(ClientSocket socket) 
	throws IOException{
		_size = socket.readLong();
		return this;
	}
	
}
